The NHibernate designer supports one-to-many, one-to-one and many-to-many associations.

To create an association, open the Toolbox and select the type of association you want to create. Then click on the entity which is the source of the association and drag to the target entity.

Collection Kinds

For collections (the to-many direction of a one-to-many association, and each direction of a many-to-many association), you must choose a collection kind. This may be one of:

If you choose the List kind, you must specify which column should be used as the list index. This must be an integer column. If you choose the Map kind, you must specify which column should be used as the map key, and the data type of the map key. Maps can use any data type as the key.

One to Many Associations

If you are creating your model by dragging tables from the database onto the designer, then the designer will automatically create one-to-many associations for you when the database contains a foreign key.

If you are creating your model using the Toolbox, you must configure the association with the name of the column containing the foreign key. To do this, select the association arrow in the designer, go to the Properties window, and edit the Key Column Name property.

Many to Many Associations

A many-to-many association depends on an association table (also known as a relation table or through table). You must configure the association with the association table name, and the foreign key columns in the association table. To do this, select the association arrow in the designer, go to the Properties window, and edit the Table Name, Source Key Column Name and Target Key Column Name properties.

Bidirectional Associations

The current version of the NHibernate designer always generates bidirectional associations: for example, if you create a one-to-many association from A to B, the generated code and mappings will also include a many-to-one association from B to A.

Eager and Lazy Loading

Collections are lazy loaded by default. To configure a collection for eager loading, select the association arrow and use the Properties window. (A one-to-many association has a single Eager Load Collection setting; a many-to-many association has two settings, one for each direction.)